/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: CanIf.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: CANIF
*
* Revision:
* --------------------------------------------------------------------------------
* v0.1: 
* --Yueting.Ben, 2024-02-23 
* --Initial release

**********************************************************************************/

/** HEADER FILES **/
#include "CanIf.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/
typedef struct
{
    uint8 sduDataBuffer[64];
    uint8 length;
    uint8 indicationFlag;
}CanifSduInfoType;


/** GLOBAL DATA DEFINITION **/


/** LOCAL DATA DEFINITION **/


/** LOCAL FUNCTION DECLARATION **/
static CanifSduInfoType CanifSduInfoTx[CANIF_TX_INDEX_MAX];
static CanifSduInfoType CanifSduInfoRx[CANIF_RX_INDEX_MAX];

/** GLOBAL FUNCTION DECLARATION **/
void CanIf_Init (
    const CanIf_ConfigType* ConfigPtr
);

Std_ReturnType CanIf_Transmit (
    PduIdType TxPduId,
    const PduInfoType* PduInfoPtr
);

Std_ReturnType CanIf_ReadRxPduData (
    PduIdType CanIfRxSduId,
    PduInfoType* CanIfRxInfoPtr
);

CanIf_NotifStatusType CanIf_ReadRxNotifStatus (
    PduIdType CanIfRxSduId
);

void CanIf_TxConfirmation (
    PduIdType CanTxPduId
);

void CanIf_RxIndication (
    const Can_HwType* Mailbox,
    const PduInfoType* PduInfoPtr
);
/** LOCAL FUNCTION **/
static CanifSduInfoType CanifSduInfoTx[CANIF_TX_INDEX_MAX] = 
{
    {0}, RESET
};
static CanifSduInfoType CanifSduInfoRx[CANIF_RX_INDEX_MAX] = 
{
    {0}, RESET
};

/** GLOBAL FUNCTION **/
void CanIf_Init (
    const CanIf_ConfigType* ConfigPtr
)
{
}


Std_ReturnType CanIf_Transmit (
    PduIdType TxPduId,
    const PduInfoType* PduInfoPtr
)
{
    Std_ReturnType retval = E_NOT_OK;
    Can_HwHandleType hth;
    Can_PduType pduInfo;

    pduInfo.id = Canif_TxPduCfg[TxPduId].canId;
    pduInfo.length = Canif_TxPduCfg[TxPduId].length;
    pduInfo.sdu = PduInfoPtr->SduDataPtr;
    
    Can_Write(hth, &pduInfo);
}


Std_ReturnType CanIf_ReadRxPduData (
    PduIdType CanIfRxSduId,
    PduInfoType* CanIfRxInfoPtr
)
{
    CanifSduInfoType* sduInfoPtr = &CanifSduInfoRx[CanIfRxSduId];
    CanIfRxInfoPtr->SduLength = sduInfoPtr->length;

    memcpy((uint8 *)(CanIfRxInfoPtr->SduDataPtr), (uint8 *)(sduInfoPtr->sduDataBuffer), sduInfoPtr->length);
}


CanIf_NotifStatusType CanIf_ReadRxNotifStatus (
    PduIdType CanIfRxSduId
)
{
    CanIf_NotifStatusType retVal = CANIF_NO_NOTIFICATION;
    CanifSduInfoType* sduInfoPtr = &CanifSduInfoRx[CanIfRxSduId];
    
    if(SET == sduInfoPtr->indicationFlag)
    {
        retVal = CANIF_TX_RX_NOTIFICATION;
        sduInfoPtr->indicationFlag = RESET;
    }
    
    return(retVal);
}

void CanIf_TxConfirmation (
    PduIdType CanTxPduId
)
{
}


void CanIf_RxIndication (
    const Can_HwType* Mailbox,
    const PduInfoType* PduInfoPtr
)
{
    PduIdType rxId;
    CanifSduInfoType* sduInfoPtr;
    
    for(rxId = CANIF_RX_INDEX_FIRST; rxId < CANIF_RX_INDEX_MAX; rxId++)
    {
        /* Id check and length check */
        if((Canif_RxPduCfg[rxId].canId == Mailbox->CanId)
            && (Canif_RxPduCfg[rxId].length == PduInfoPtr->SduLength))
        {
            sduInfoPtr = &CanifSduInfoRx[rxId];
            sduInfoPtr->length = PduInfoPtr->SduLength;
            memcpy((uint8 *)(sduInfoPtr->sduDataBuffer), (uint8 *)(PduInfoPtr->SduDataPtr), PduInfoPtr->SduLength);
            sduInfoPtr->indicationFlag = SET;

            Canif_RxIndicationFuncCfg[rxId](Canif_RxPduCfg[rxId].UpperPduId, PduInfoPtr);
            
            break;
        }
    }
}

